package is.api.demo.isgateway.support;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**********************************************************************
 * &lt;p&gt;文件名：ResponseCodeEnum.java &lt;/p&gt;
 * &lt;p&gt;文件描述：(描述该文件做什么)
 * @project_name：rest-api-demo
 * @author zengshunyao
 * @email 641995773@qq.com
 * @date 2025/11/25 11:27 下午
 * @history
 * @department：政务事业部
 * Copyright ChengDu Funi Cloud Code Technology Development CO.,LTD 2014
 *                    All Rights Reserved.
 */
public enum OpenResponseCodeEnum {

    OK("OPEN-1000", "平台已成功处理了请求"),
    OPEN1001("OPEN-1001", "平台已接受请求，但尚未处理"),
    OPEN1002("OPEN-1002", "平台身份验证,未授权"),
    OPEN1003("OPEN-1003", "平台签名验证,未授权"),
    OPEN1004("OPEN-1004", "平台拒绝请求"),
    OPEN1005("OPEN-1005", "平台找不到请求的资源，接口路径错误"),
    OPEN1006("OPEN-1006", "平台找不到请求的资源，接口未授权"),
    OPEN1007("OPEN-1007", "平台禁用了请求中指定的方法"),
    OPEN1008("OPEN-1008", "请求时发生超时"),
    OPEN1009("OPEN-1009", "请求的资源已永久删除"),
    OPEN1010("OPEN-1010", "请求头格式不支持"),
    OPEN1011("OPEN-1011", "请求内容格式不支持"),
    OPEN1012("OPEN-1012", "服务器遇到错误，无法完成请求");

    private static final Map<String, OpenResponseCodeEnum> cacheMap = new ConcurrentHashMap<>();

    static {
        Arrays.stream(OpenResponseCodeEnum.values()).forEach(e -> cacheMap.put(e.getCode(), e));
    }

    private final String code;
    private final String text;

    OpenResponseCodeEnum(String code, String text) {
        this.code = code;
        this.text = text;
    }

    /**
     * 根据code获取枚举
     *
     * @param code 编码
     * @return 枚举
     */
    public static OpenResponseCodeEnum getResponseCodeEnumByCode(String code) {
        if (Objects.isNull(code)){
            return null;
        }
        final OpenResponseCodeEnum responseCodeEnum = cacheMap.get(code);
        return responseCodeEnum;
    }

    /**
     * 根据code获取text
     *
     * @param code 编码
     * @return 中文释义
     */
    public static String getResponseTextByCode(String code) {
        final OpenResponseCodeEnum responseCodeEnum = getResponseCodeEnumByCode(code);
        if (Objects.nonNull(responseCodeEnum)) {
            return responseCodeEnum.getText();
        }
        return null;
    }

    public static boolean isOk(String code) {
        final OpenResponseCodeEnum openResponseCodeEnum = getResponseCodeEnumByCode(code);
        return Objects.equals(OpenResponseCodeEnum.OK, openResponseCodeEnum);
    }

    public String getCode() {
        return code;
    }

    public String getText() {
        return text;
    }
}
